package com.demo.woziqi.dao;

import com.demo.woziqi.entity.User;
import com.demo.woziqi.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 宋明松
 * @Version V1.0.0
 * @Date 2022-06-01
 */
public class ResultDao {
    /**
     * 添加对局结果方法
     *
     * @param blackID 黑棋用户的ID
     * @param whiteID 白棋用户的ID
     * @param str     对局结果:(黑胜，白胜，和棋)
     * @return 返回true表示更新成功，返回false表示更新失败
     */
    public boolean setResult(int blackID, int whiteID, String str) {
        String sql = "INSERT INTO result(black_id,white_id,result,game_time) VALUES(?,?,?,NOW())";
        Connection conn = null;
        try {
            conn = DBUtil.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try (
             PreparedStatement stat = conn.prepareStatement(sql)) {
            stat.setInt(1, blackID);
            stat.setInt(2, whiteID);
            stat.setString(3, str);
            if (stat.executeUpdate() == 1) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

    /**
     * 查询历史对战记录 我方为黑棋时
     *
     * @param user
     * @return
     */
    public List<String> getResultBlack(User user) {
        List<String> resList = new ArrayList<>();
        String sql = "SELECT * from result INNER JOIN `user` on result.white_id = `user`.id WHERE black_id=?";
        Connection conn = null;
        try {
            conn = DBUtil.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try (
             PreparedStatement stat = conn.prepareStatement(sql)) {
            stat.setInt(1, user.getId());
            ResultSet rs = stat.executeQuery();
            while (rs.next()) { // 返回的有数据,保存信息
                resList.add("对方用户：" + rs.getString("username"));
                resList.add("对局结果：" + rs.getString("result"));
                resList.add("对局时间：" + rs.getString("game_time"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            return resList; // 返回null resList
        }
        return resList; // 返回用户信息
    }

    /**
     * 查询历史对战记录 我方为白棋时
     *
     * @param user
     * @return
     */
    public List<String> getResultWhite(User user) {
        List<String> resList = new ArrayList<>();
        String sql = "SELECT * from result INNER JOIN `user` on result.black_id = `user`.id WHERE white_id=?";
        Connection conn = null;
        try {
            conn = DBUtil.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try (
                PreparedStatement stat = conn.prepareStatement(sql)) {
            stat.setInt(1, user.getId());
            ResultSet rs = stat.executeQuery();
            while (rs.next()) { // 返回的有数据,保存信息
                resList.add("对方用户：" + rs.getString("username"));
                resList.add("对局结果：" + rs.getString("result"));
                resList.add("对局时间：" + rs.getString("game_time"));
            }

        } catch (Exception e) {
            e.printStackTrace();
            return resList; // 返回null resList
        }
        return resList; // 返回用户信息
    }
}
